using System;
using System.Windows.Input;

namespace Common.Command
{
	public class SimpleDelegateCommand : ICommand
	{
		private Func<bool> canExecute;

		private Action executeAction;

		private bool canExecuteCache;

		public event EventHandler CanExecuteChanged;

		public SimpleDelegateCommand(Action executeAction, Func<bool> canExecute)
		{
			this.executeAction = executeAction;
			this.canExecute = canExecute;
		}

		public SimpleDelegateCommand(Action execute)
			: this(execute, null)
		{
		}

		public bool CanExecute(object parameter)
		{
			if (canExecute == null)
			{
				return true;
			}
			bool flag = canExecute();
			if (canExecuteCache != flag)
			{
				canExecuteCache = flag;
				RaiseCanExecuteChanged();
			}
			return canExecuteCache;
		}

		public void RaiseCanExecuteChanged()
		{
			if (this.CanExecuteChanged != null)
			{
				this.CanExecuteChanged(this, EventArgs.Empty);
			}
		}

		public void Execute(object parameter)
		{
			executeAction();
		}
	}
}
